#!/usr/bin/env node --redirect-warnings=/dev/null

const { execFileSync } = require("child_process");
let { GITHUB_ACCESS_TOKEN } = process.env;
const FIXES_REGEX = /(fixes|closes|completes) (.+[/#]\d+.*)$/im;

main();

async function main() {
  // Get the last two preview tags
  const [newTag, oldTag] = execFileSync(
    "git",
    ["tag", "--sort", "-committerdate"],
    { encoding: "utf8" },
  )
    .split("\n")
    .filter((t) => t.startsWith("v") && t.endsWith("-pre"));

  // Print the previous release
  console.log(`Changes from ${oldTag} to ${newTag}\n`);

  if (!GITHUB_ACCESS_TOKEN) {
    try {
      GITHUB_ACCESS_TOKEN = execFileSync("gh", ["auth", "token"]).toString();
    } catch (error) {
      console.log(error);
      console.log("No GITHUB_ACCESS_TOKEN, and no `gh auth token`");
      process.exit(1);
    }
  }

  // Get the PRs merged between those two tags.
  const pullRequestNumbers = getPullRequestNumbers(oldTag, newTag);

  // Get the PRs that were cherry-picked between main and the old tag.
  const existingPullRequestNumbers = new Set(
    getPullRequestNumbers("main", oldTag),
  );

  // Filter out those existing PRs from the set of new PRs.
  const newPullRequestNumbers = pullRequestNumbers.filter(
    (number) => !existingPullRequestNumbers.has(number),
  );

  // Fetch the pull requests from the GitHub API.
  console.log("Merged Pull requests:");
  for (const pullRequestNumber of newPullRequestNumbers) {
    const webURL = `https://github.com/zed-industries/zed/pull/${pullRequestNumber}`;
    const apiURL = `https://api.github.com/repos/zed-industries/zed/pulls/${pullRequestNumber}`;

    const response = await fetch(apiURL, {
      headers: {
        Authorization: `token ${GITHUB_ACCESS_TOKEN}`,
      },
    });

    // Print the pull request title and URL.
    const pullRequest = await response.json();
    const releaseNotesHeader = /^\s*Release Notes:(.+)/ims;

    let releaseNotes = pullRequest.body || "";
    const captures = releaseNotesHeader.exec(releaseNotes);
    const notes = captures ? captures[1] : "MISSING";
    const skippableNoteRegex = /^\s*-?\s*n\/?a\s*/ims;

    if (skippableNoteRegex.exec(notes) != null) {
      continue;
    }
    console.log("*", pullRequest.title);
    console.log("  PR URL:    ", webURL);

    // If the pull request contains a 'closes' line, print the closed issue.
    const fixesMatch = (pullRequest.body || "").match(FIXES_REGEX);
    if (fixesMatch) {
      const fixedIssueURL = fixesMatch[2];
      console.log("  Issue URL:    ", fixedIssueURL);
    }

    releaseNotes = notes.trim().split("\n");
    console.log("  Release Notes:");

    for (const line of releaseNotes) {
      console.log(`    ${line}`);
    }

    console.log();
  }
}

function getPullRequestNumbers(oldTag, newTag) {
  const pullRequestNumbers = execFileSync(
    "git",
    ["log", `${oldTag}..${newTag}`, "--oneline"],
    { encoding: "utf8" },
  )
    .split("\n")
    .filter((line) => line.length > 0)
    .map((line) => {
      const match = line.match(/#(\d+)/);
      return match ? match[1] : null;
    })
    .filter((line) => line);

  return pullRequestNumbers;
}
